'''
    带Cookie方法很多, 有直接在header复制粘贴的, 也有文件中读取cookie的, 这些参见gitbook;
    CookieJar能全自动获取cookie并在open(url)时自动把cookie带过去;

本例子是利用cookie登录人人网:
    这种方法也就只在人人网管用, 要靠谱还是得用selenium

'''


import urllib.request as ur
import urllib.parse as up
from http.cookiejar import CookieJar

cookie = CookieJar()

cookie_handler = ur.HTTPCookieProcessor(cookie)

opener = ur.build_opener(cookie_handler)

opener.addheaders = [("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36")]

data = {"email":"mr_mao_hacker@163.com", "password":"alaxxxxxime"} 

postdata = up.urlencode(data).encode('utf8')

request = ur.Request("http://www.renren.com/PLogin.do", data = postdata)

opener.open(request)

response = opener.open("http://www.renren.com/410043129/profile")

print(response.read().decode('utf8'))

